package com.sufen.seckill.study.FaceCoding;

import com.baomidou.mybatisplus.extension.api.R;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class DoubleThreadPrint100 {
    public static void main(String[] args) {
        Runnable task=new Print100();
        Thread t1=new Thread(task,"A");
        Thread t2=new Thread(task,"B");
        t1.start();
        t2.start();
    }

}

class Print100 implements Runnable{

    public int num=0;
    ReentrantLock lock=new ReentrantLock();
    Condition condition=lock.newCondition();

    @Override
    public void run() {
        while(num<100){
            lock.lock();
            num++;
            System.out.println(num+","+Thread.currentThread().getName());
            condition.signal();
            try {
                if(num<100){
                    condition.await();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        }
    }
}
